<!--
 * @Description: 
 * @Author: 梁萌
 * @Date: 2020-12-12 12:09:01
 * @LastEditTime: 2020-12-19 16:52:27
 * @FilePath: \vision\src\components\Hot.vue
-->
<template>
  <div class="com-container">
    <div class="com-chart" ref="hot_ref"></div>
    <span class="iconfont arr-left" @click="toLeft" :style="comSytle">&#xe6ef;</span>
    <span class="iconfont arr-right" @click="toRight" :style="comSytle">&#xe6ed;</span>
    <span class="cat-name" :style="comSytle">{{catName}}</span>
  </div>
</template>

<script>
import { mapState } from "vuex";
import { getThemeValue } from "@/utils/theme_utils";
export default {
  data() {
    return {
      chartIntance: null,
      allData: null, //服务器返回的数据
      currentIndex: 0, //当前所展示出的一级分类数据索引
      titleFontSize: 0, //全局字体大小
    };
  },
  created() {
    //组件创建完成后进行回调函数的注册
    this.$socket.registerCallBack("hotproductData", this.getData);
  },
  computed: {
    //一级类别名称
    catName() {
      if (this.allData) {
        return this.allData[this.currentIndex].name;
      } else {
        return "";
      }
    },
    //span的样式
    comSytle() {
      return {
        fontSize: this.titleFontSize + "px",
        color: getThemeValue(this.theme).titleColor,
      };
    },
    ...mapState(["theme"]),
  },
  mounted() {
    this.initChart();
    //this.getData();
    //发送数据给服务器,获取数据
    this.$socket.send({
      action: "getData",
      socketType: "hotproductData",
      chartName: "hotproduct",
      value: "",
    });
    window.addEventListener("resize", this.screenAdapter);
    this.screenAdapter();
  },
  destroyed() {
    window.removeEventListener("resize", this.screenAdapter);
    //在组件销毁时进行回调函数取消
    this.$socket.unRegisterCallBack("hotproductData");
  },
  methods: {
    //初始化echartsInstance对象
    initChart() {
      this.chartIntance = this.$echarts.init(this.$refs.hot_ref, this.theme); //初始化echarts实例对象
      //对图表初始化配置的控制
      const initOption = {
        title: {
          text: "▎热销商品的占比",
          left: 20,
          top: 20,
        },
        legend: {
          top: "15%",
          icon: "circle",
        },
        tooltip: {
          show: true,
          formatter: (arg) => {
            const thirdCategory = arg.data.children; //三级分类数据

            let total = 0; //三级分类总数
            thirdCategory.forEach((item) => {
              total += item.value;
            });

            let retStr = ""; //三级分类字符串拼接
            thirdCategory.forEach((item) => {
              retStr += `
              ${item.name}:${parseInt((item.value / total) * 100) + "%"}
              <br/>
              `;
            });
            return retStr;
          },
        },
        series: [
          {
            type: "pie",
            label: {
              show: false,
            },
            emphasis: {
              label: {
                show: true,
              },
              labelLine: {
                show: false,
              },
            },
          },
        ],
      };
      this.chartIntance.setOption(initOption);
    },

    //获取服务器数据
    getData(ret) {
      //const { data: ret } = await this.$http.get("hotproduct");
      this.allData = ret;
      this.updateChart();
    },

    //更新图表
    updateChart() {
      //图例数据
      const legendData = this.allData[this.currentIndex].children.map(
        (item) => {
          return {
            name: item.name,
          };
        }
      );

      const seriesData = this.allData[this.currentIndex].children.map(
        (item) => {
          return {
            name: item.name,
            value: item.value,
            children: item.children,
          };
        }
      );
      const dataOption = {
        legend: {
          data: legendData,
        },
        series: [
          {
            data: seriesData,
          },
        ],
      };

      this.chartIntance.setOption(dataOption);
    },

    //当浏览器大小变化时进行屏幕适配
    screenAdapter() {
      this.titleFontSize = (this.$refs.hot_ref.offsetWidth / 100) * 3.6;
      //和分辨率大小相关配置
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: this.titleFontSize,
          },
        },
        legend: {
          itemWidth: this.titleFontSize,
          itemHeight: this.titleFontSize,
          itemGap: this.titleFontSize / 2,
          textStyle: {
            fontSize: this.titleFontSize / 2,
          },
        },
        series: [
          {
            radius: this.titleFontSize * 4.5,
            center: ["50%", "60%"],
          },
        ],
      };

      this.chartIntance.setOption(adapterOption);
      this.chartIntance.resize();
    },

    //向左箭头点击事件
    toLeft() {
      this.currentIndex--;
      if (this.currentIndex < 0) {
        this.currentIndex = this.allData.length - 1;
      }
      this.updateChart();
    },

    //向右箭头点击事件
    toRight() {
      this.currentIndex++;
      if (this.currentIndex > this.allData.length - 1) {
        this.currentIndex = 0;
      }
      this.updateChart();
    },
  },
  watch: {
    theme() {
      this.chartIntance.dispose();
      this.initChart();
      this.screenAdapter();
      this.updateChart();
    },
  },
};
</script>

<style lang='less' scoped>
.arr-left {
  position: absolute;
  left: 10%;
  top: 50%;
  transform: translateY(-50%);
  cursor: pointer;
  color: white;
}

.arr-right {
  position: absolute;
  right: 10%;
  top: 50%;
  transform: translateY(-50%);
  cursor: pointer;
  color: white;
}

.cat-name {
  position: absolute;
  left: 80%;
  bottom: 20px;
  color: white;
}
</style>